#一.列表练习题初始
#1.创建一个列表list1
list1 = [1, 2, 3, 4, 5, 8]
#2.向list1添加一个元素6
list1.append(6)
print(list1)
#3.删除元素3
list1.remove(3)
print(list1)
#4.
list1[1] = 8
#5.
print(list1.index(8))
#6.
list1 = [1, 2, 3, 4, 5, 8, 6]
for i in list1:
    print(i,end=' ')
print()
#7.
list1 = [1,2,3,4,5,8]
list2 = [10,11,12]
for i in list2:
    list1.append(i)
print(list1)
#8.
list2 = [10,11,12]
list1 = list1+list2
print(list1)
#9.
print(list1[2:5:1])
#10
list1 = [0,1,2,3]
list1[2:3] = [13,14]
print(list1)
from dask.array import delete
from num2words import num2words
from numba.typed.listobject import new_list
from sympy.physics.units import length
from toolz.curried import remove

#二.列表练习题进阶
#1.
my_list = [3,'apple',9,'banana',7,'cherry',2,"date",5,'elderberry']
my_list.append('fig')
print(my_list)
#2.
my_list.remove('banana')
print(my_list)
#3.
my_list[my_list.index(7)] = 'grape'
print(my_list)
#4.
print(my_list.index('cherry'))
#5.
for i in my_list:
    print(i)
#6.
my_list.insert(my_list.index('cherry')+1,'kiwi')
print(my_list)
#7.
print(my_list[2])
#8.
print(my_list[-2])
#9.
print(my_list[2:7:1])
#10.
print(my_list[::-1])
#11.
my_list = [3, 'apple', 9, 'banana', 7, 'dherry', 2, "cdate", 5, 'elderberry']
# 提取所有字符串元素
strings = [item for item in my_list if isinstance(item, str)]
# 对字符串进行排序
strings.sort()
#  创建迭代器用于按顺序获取排序后的字符串
string_iter = iter(strings)
# 构建新列表：数字保留原位，字符串位置用排序后的字符串填充
result = []
for item in my_list:
    if isinstance(item, int):
        result.append(item)  # 保留数字在原位
    else:
        result.append(next(string_iter))  # 用排序后的字符串填充
print(result)
#12.
my_list = [3, 'apple', 9, 'banana', 7, 'dherry', 2, "cdate", 5, 'elderberry']
for item in my_list:
    if isinstance(item,int):
         my_list[my_list.index(item)] = num2words(item)
print(my_list)

#三.列表练习题-复杂
# 1.
my_list=[]
for item in range(1,101):
    if item % 2 == 0:
        my_list.append(item)
print(my_list)
#2.
for i in range(0,len(my_list)):
    my_list[i] = pow(my_list[i],2)
print(my_list)
#3.
for i in range(len(my_list)-1,-1,-1):
    if my_list[i] > 1000:
        del my_list[i]
print(my_list)
#4.
my_list.reverse()
print(my_list)
#5.
print(my_list[0:10])
#6.
my_list.extend([101,102,103])
print(my_list)
#7.
avg = sum(my_list) / len(my_list)
print(f"平均值：{avg:.2f}")
#8.
print(max(my_list),min(my_list))
#9.
my_list = [1,2,11,3,'sad','ss',4,11]
print(my_list.index(11))
#10.
for item in my_list:
    if isinstance(item,int):
        my_list[my_list.index(item)] = f'{item}'
print(my_list)
#11.
my_list = [1,2,110,3,'sad','ss',4,11,90]
for i in range(0,len(my_list)):
    if isinstance(my_list[i],int):
        my_list[i] = float(my_list[i])
print(my_list)
#12.
my_list = [1,2,110,3,'sad','ss',4,11,90]
new_list = []
for i in range(0,len(my_list)):
    if isinstance(my_list[i],int) and my_list[i] > 50:
        new_list.append(my_list[i])
print(new_list)
#13.
my_list = [1,2,110,3,'sad','ss',4,11,90]
my_list.insert(2,99)
print(my_list)
#14.
my_list.pop()
print(my_list)
#15.
my_list = [1,2,110,3,4,11,90]
new_list = [x * 2 for x in my_list]
print(new_list)
#16.
my_list2 = [50,60,40,30]
big_list = []
for i in range(0,len(my_list2)):
    if my_list2[i] > 50 :
        big_list.append(my_list2[i])
small_list = list(set(my_list2)-set(big_list))
print(small_list,big_list)
#17.
my_list = [1,2,110,3,'sad','ss',4,11,90]
dict_new = {}
i = 0
for item in my_list:
    dict_new[item] = i
    i+=1
print(dict_new)
# #18.
my_list = [1,2,110,3,'sad','ss',4,11,90]
for i,item, in enumerate(my_list):
    print(item,i)
#19.
from random import randint
import random
my_list = [1,2,110,3,'sad','ss',4,11,90]
i = random.randint(0,len(my_list))
print(my_list[i])
#20.
my_list = ["apple", "banana", "orange", 42, 3.14]
# 打开文件并写入
with open("output.txt", "w") as file:
    for item in my_list:
        file.write(str(item) + "\n")  # 转换为字符串并添加换行符